module TableView where

import Graphics.UI.Gtk
import Graphics.UI.Gtk.ModelView as MV
import qualified Data.ByteString.Lazy.Char8 as L

data ViewState = V 
    {
      mainWidget :: Widget
    , treeView :: MV.TreeView
    , maxLines :: SpinButton
    , searchE :: Entry
    , searchPB :: Button
    , searchNB :: Button
    , groupB :: ToggleButton
    , groupE :: SpinButton
    }

new treeM loadSaveW = do
  vbox <- vBoxNew False 2
  hbox <- hBoxNew False 2
  l <- labelNew (Just "Max. Lines: ")

  sl <- labelNew (Just "Search in Table: ")
  searchE <- entryNew
  searchNB <- buttonNewFromStock stockGoDown
  searchPB <- buttonNewFromStock stockGoUp

  maxLines <- spinButtonNewWithRange 1 50000 1
  spinButtonSetValue maxLines 5000.0 

  groupL <- labelNew (Just "Column: ")
  groupE <- spinButtonNewWithRange 0 50000 1
  spinButtonSetWrap groupE True
  spinButtonSetSnapToTicks groupE True
  spinButtonSetValue groupE 0
  groupB <- toggleButtonNewWithLabel "Group"

  v1 <- vSeparatorNew
  v2 <- vSeparatorNew
  v3 <- vSeparatorNew

  boxPackStart hbox loadSaveW PackNatural 2  
  boxPackStart hbox v1 PackNatural 2  
  boxPackStart hbox sl PackNatural 2  
  boxPackStart hbox searchE PackNatural 2  
  boxPackStart hbox searchPB PackNatural 2  
  boxPackStart hbox searchNB PackNatural 2  
  boxPackStart hbox v2 PackNatural 2  
  boxPackStart hbox groupL PackNatural 2  
  boxPackStart hbox groupE PackNatural 2  
  boxPackStart hbox groupB PackNatural 2                 
  boxPackStart hbox v3 PackNatural 2  
  boxPackEnd hbox maxLines PackNatural 2  
  boxPackEnd hbox l PackNatural 2

  treeV <- MV.treeViewNewWithModel treeM
  scrollWin <- scrolledWindowNew Nothing Nothing
  containerAdd scrollWin treeV

  boxPackStart vbox scrollWin PackGrow 2
  boxPackStart vbox hbox PackNatural 2

  set treeV [ MV.treeViewHeadersClickable := True
            , MV.treeViewHeadersVisible := True
--            , MV.treeViewEnableGridLines := MV.TreeViewGridLinesHorizontal 
            , MV.treeViewReorderable := True
            , MV.treeViewRulesHint := True
            , MV.treeViewEnableSearch := True
--            , MV.treeViewRubberBanding := True
            ]
  return $ V (toWidget vbox) treeV maxLines searchE searchPB searchNB groupB groupE

add_col treeV listM row_sel title scolid = do
  strCol <- MV.treeViewColumnNew
  strColRenderer <- MV.cellRendererTextNew
  set strColRenderer [ MV.cellTextEditable := True ]
  MV.treeViewColumnPackStart strCol strColRenderer True
  MV.treeViewColumnSetSortColumnId strCol scolid  
  MV.cellLayoutPackStart strCol strColRenderer True
  MV.treeViewColumnSetResizable strCol True
  MV.cellLayoutSetAttributes strCol strColRenderer listM $ \row -> 
      [MV.cellText := (row_sel row) ]
  MV.treeViewColumnSetTitle strCol title
  MV.treeViewAppendColumn treeV strCol
  return ()
